package Concurent;

import java.util.concurrent.*;

/**
 * Created by root on 17-6-8.
 */
public class ThreadPoolDemo1 {
    public static void main(String[] args) {
        ExecutorService service = new ThreadPoolExecutor(5, 10, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5), new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                System.out.println("线程池忙不过来了，多出来的就拒绝吧");
            }

        });
        for (int i=1;i<20;i++) {
            service.execute(new T12(i));   //不返回
            //submit1
            //Future future = service.submit(new T12(i));     //任务结束后会返回一个Future对象
            //print(future.get()); //执行完成会返回null
            //submit2
            //

            //service.shutdown();    //并不会直接关闭线程池，只是不加任务了，等线程都执行完了再关
            //service.shutdownNow();   //直接强制关闭
        }
    }
}

class T14 implements Callable<String> {
    @Override
    public String call() throws Exception {
        try {
            Thread.sleep(10000);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return "return from T14";
    }
}




class T12 implements Runnable{
    int id = 0;
    public T12(int id) {
        this.id = id;
    }

    @Override
    public void run() {
        System.out.println("线程"+id +"被创建出来了");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}